var path = require('path');

var express = require('express');
// var favicon = require('serve-favicon');
var request = require('request');

var clc = require('cli-color');

var config = require(path.join(
    __dirname,
    'config.json'
));

var app = express();

var api = require(path.join(
    __dirname,
    'handlers', 'api'
));
var pages = require(path.join(
    __dirname,
    'handlers', 'pages'
));

app.get('/test', function (req, res) {
  res.send('Hello World!');
});

// 后端 api 转发
app.use('/api', api);

/*
// favicon 文件
app.use(favicon(path.join(
    __dirname,
    'public',
    'favicon.ico'
)));
*/
/*
var options = {
    dotfiles: 'ignore',
    etag: true,
    extensions: ['css', 'js', 'html', 'txt'],
    index: 'index.html',
    lastModified: true,
    maxAge: '1d',
    setHeaders: function (res, path, stat) {
        res.set('x-timestamp', Date.now());
        res.header('Cache-Control', 'public, max-age=1d');
    }
};
*/
// 静态资源文件(主要是 vue 打包后)
app.use('/static', express.static(path.join(
    __dirname,
    'public', 'static'
)));

// 其他文件(主要是 html 页面)
app.use('', pages);

var listen_port = 20001,
    listen_host = '127.0.0.1';
if(config.debug) {
    listen_host = '0.0.0.0'
}
var server = app.listen(listen_port, listen_host, function() {
    var host = server.address().address;
    var port = server.address().port;

    if(host == '::') {
        host = '127.0.0.1';
    }
    console.log('Example app listening at http://%s:%s', host, port);
});